3 struct MapEdges: View {
10 let arrowheadSize = CGFloat(10.0)
13 ForEach(edges, id: \.id) { edge in
16 // First we transform edges from percentage to map coordinates
17 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
18 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
20 let slope = (destination.y - origin.y) / (destination.x - origin.x)
21 let angle = atan(slope)
22 let multiplier = CGFloat(slope < 0 ? -1.0 : 1.0)
23 let upperAngle = angle - CGFloat.pi / 4.0
24 let lowerAngle = angle + CGFloat.pi / 4.0
26 let offsetOrigin = CGPoint(
27 x: origin.x + multiplier * (vertexSize.width / 2.0) * cos(angle),
28 y: origin.y + multiplier * (vertexSize.height / 2.0) * sin(angle))
29 let offsetDestination = CGPoint(
30 x: destination.x - multiplier * (vertexSize.width / 2.0) * cos(angle),
31 y: destination.y - multiplier * (vertexSize.height / 2.0) * sin(angle))
33 path.move(to: offsetOrigin)
34 path.addLine(to: offsetDestination)
37 path.move(to: offsetDestination)
40 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
42 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
44 path.move(to: offsetDestination)
47 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
49 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
51 path.move(to: offsetDestination)
54 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
55 ).stroke(Color.map.vertexColor, lineWidth: lineWidth)
59 func h(_ dimension: CGFloat) -> CGFloat {
60 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
63 func w(_ dimension: CGFloat) -> CGFloat {
64 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
70 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
71 vertexSize: CGSize(width: 25.0, height: 25.0),
74 id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2),